package com.colorfruit.admin.business.service.impl;

import com.colorfruit.admin.business.dao.*;
import com.colorfruit.admin.business.service.LogicService;
import com.colorfruit.admin.pojo.entity.DriverInfo;
import com.colorfruit.admin.pojo.entity.GlobalProcessionControl;
import com.colorfruit.admin.pojo.entity.LogicTransportInfo;
import com.colorfruit.admin.pojo.enums.DistributeStatusEnum;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.enums.GlobalStatusEnum;
import com.colorfruit.admin.pojo.vo.purchase.PurchaseTotalAddressInfoVo;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class LogicServiceImpl implements LogicService {

    @Autowired
    DriverInfoMapper driverInfoMapper;

    @Autowired
    GlobalProcessionControlMapper globalProcessionControlMapper;

    @Autowired
    PurchaseTotalAddressInfoMapper purchaseTotalAddressInfoMapper;

    @Autowired
    LogicTransportInfoMapper logicTransportInfoMapper;

    @Autowired
    PayBusinessOrderMapper payBusinessOrderMapper;

    @Override
    public BaseResult getLogicTransportInfo(Long pickGoodsAddressId) {
        log.info("[service] [IN] getLogicTransportInfo param : pickGoodsAddressId = {}", pickGoodsAddressId);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        LogicTransportInfo logicTransportInfo = logicTransportInfoMapper.getLogicTransportInfo(recentProcession.getId(), pickGoodsAddressId);
        log.info("[service] [OUT] getLogicTransportInfo success ");
        return new BaseResult(logicTransportInfo);
    }

    @Override
    public BaseResult addDriverInfo(Long pickGoodsAddressId,  String driver, String phone, String carNumber) {
        log.info("[service] [IN] addDriverInfo param : pickGoodsAddressId = {}, driver = {} , phone = {}, carNumber = {}",pickGoodsAddressId,  driver, phone, carNumber);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        DriverInfo driverInfo = driverInfoMapper.getDriverInfo(recentProcession.getId(), pickGoodsAddressId);
        if (driverInfo != null){
            return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "目前一个地址只支持一个司机送货");
        }
        driverInfo = new DriverInfo();
        driverInfo.setGlobalControlId(recentProcession.getId());
        driverInfo.setRecordStatus((byte)2);
        driverInfo.setDriverName(driver);
        driverInfo.setDriverPhone(phone);
        driverInfo.setPickGoodsAddressId(pickGoodsAddressId);
        driverInfo.setArrivalTime(null);
        driverInfo.setCreateTime(new Date());
        driverInfo.setUpdateTime(new Date());
        driverInfo.setCarNum(carNumber);
        driverInfoMapper.insert(driverInfo);
        log.info("[service] [OUT] addDriverInfo success ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }

    @Override
    public BaseResult selectDriverInfo(Long pickAddressId) {
        log.info("[service] [IN] selectDriverInfo param : pickAddressId = {}", pickAddressId);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        DriverInfo logicInfo = driverInfoMapper.getDriverInfo(recentProcession.getId(), pickAddressId);
        log.info("[service] [OUT] selectDictListByType success ");
        return new BaseResult(logicInfo);
    }

    @Override
    public BaseResult selectProductList(Long pickAddressId) {
        log.info("[service] [IN] selectProductList param : pickAddressId = {}", pickAddressId);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        List<PurchaseTotalAddressInfoVo> purchaseTotalAddressInfoVos = purchaseTotalAddressInfoMapper.selectTotalAddressList(recentProcession.getId());
        log.info("[service] [OUT] selectProductList success ");
        return new BaseResult(purchaseTotalAddressInfoVos);
    }

    @Override
    public BaseResult updateTransportNum(Long logicTransportInfoId, Long purchaseTotalAddressInfoId, int num) {
        log.info("[service] [IN] selectProductList param : purchaseTotalAddressInfoId = {}, num = {}", purchaseTotalAddressInfoId, num);

        LogicTransportInfo logicTransportInfo = logicTransportInfoMapper.selectByPrimaryKey(logicTransportInfoId);
        if (logicTransportInfo.getRecordStatus() == (byte)1){
            purchaseTotalAddressInfoMapper.updateTransportNum(purchaseTotalAddressInfoId, num);
        }else {
            return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "已运输中的数据不能变更数量");
        }
        log.info("[service] [OUT] selectProductList success ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }

    @Override
    public BaseResult generateLogicInfo(Long logicTransportInfoId) {
        log.info("[service] [IN] selectProductList param : logicTransportInfoId = {}", logicTransportInfoId);

        logicTransportInfoMapper.updateLogicStatus(logicTransportInfoId, (byte) 2 );
        LogicTransportInfo logicTransportInfo = logicTransportInfoMapper.selectByPrimaryKey(logicTransportInfoId);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        if (recentProcession.getRecordStatus() != GlobalStatusEnum.TRANSPORT.getCode()){
            return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "非运输阶段,无法生成运输单");
        }
        //设置订单未发货状态
        payBusinessOrderMapper.sendProduct(logicTransportInfo.getPickAddressId(), recentProcession.getId());
        log.info("[service] [OUT] selectProductList success ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }


}
